package com.kuang.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer02 {
    public static void main(String[] args) throws Exception {
        //1、创建服务
        ServerSocket serverSocket = new ServerSocket(9999);

        //2、监听客户端的链接
        Socket socket = serverSocket.accept();

        //3、获取输入流
        InputStream inputStream = socket.getInputStream();

        //4、文件输出(文件管道流)
        FileOutputStream fileOutputStream = new FileOutputStream(new File("Net/src/receive.png"));
        byte[] buffer = new byte[1024];
        int len;

        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }

        //通知客户端我接收完毕了
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("接收成功".getBytes());
        //关闭资源
        outputStream.close();
        fileOutputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}
